/* 
 * File:   client.c
 * Author: cuongnm
 *
 * Created on October 6, 2009, 12:28 AM
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define SERV_UDP_PORT 30346
#define SIZE 128
#define SERV_HOST_ADDR "192.168.106.216"

/*
 * 
 */
int readable_timeo(int fd, int sec){
     fd_set rset;
     struct timeval tv;
     FD_ZERO(&rset);
     FD_SET(fd, &rset);
     tv.tv_sec = sec;
     tv.tv_usec = 0;
     return (select(fd + 1, &rset, NULL, NULL, &tv));
         /* > 0 if descriptor is readable */
}

int main(int argc, char** argv) {
    int                  sockfd, servlen, n;
    struct sockaddr_in   cli_addr, serv_addr;
    char                 name[60], msg[SIZE];
    char                 server_addr[SIZE];
    int                   server_port;
    int                  i_numberReceived;
    strcpy(server_addr,argv[1]);
    //server_addr =argv[1];
    server_port =atoi(argv[2]);
    //name = argv[1];
    strcpy(name,"Bonjour");
    i_numberReceived=0;

    /* Fill in the structure "serv_addr" with the address of the
     * server that we want to send to */

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family      = AF_INET;
    //serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
    //serv_addr.sin_port        = htons(SERV_UDP_PORT);
    serv_addr.sin_addr.s_addr = inet_addr(server_addr);
    serv_addr.sin_port        = htons(server_port);
    /* Open a UDP socket (an Internet datagram socket) */
    //printf("succes\n");
    
    if ( (sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        printf("client: can't open sock stream socket");

    /* Bind any local address for us. */

    bzero((char *) &cli_addr, sizeof(cli_addr));  /* zero out */
    cli_addr.sin_family        = AF_INET;
    /* In assigning a local address for the client using bind,
     * we set the Internet address to INADDR_ANY and the 16-bit Internet port
     * to zero. Setting the port to zero causes the system to assign the client
     * some anused port in the range 1024 through 5000.*/
    cli_addr.sin_addr.s_addr   = htonl(INADDR_ANY);
    cli_addr.sin_port          = htons (0);
    if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0)
        printf("client: can't bind local address");

    servlen = sizeof(serv_addr);
    if (sendto(sockfd, name, strlen(name), 0, (struct sockaddr *) &serv_addr, servlen)!= strlen(name))
        printf("client: sendto error \n");
    //ajouter ici
    if (readable_timeo(sockfd, 5) == 0) {
    fprintf(stderr, "Timeout: send 1 message(s) : receved %d message(s)\n",i_numberReceived);
    close(sockfd);
    exit(0);
    } 
    //
    
    else{
        for(;;)
        {
         if (readable_timeo(sockfd, 5) == 0) {
         //fprintf(stderr, "socket timeout\n");
         fprintf(stderr, "Timeout: send 1 message(s) : receved %d message(s)\n",i_numberReceived);
         close(sockfd);
         exit(0);
        } 
        if ((n = recvfrom(sockfd,msg,SIZE,0,(struct sockaddr *) 0,(int *)0)) < 0)
        {
        printf("client: recvfrom error \n");
        
        };
        msg[n]='\0';
        i_numberReceived++;
        printf("%s \n", msg);
        if(strcmp(msg,"Au revoir")==0){
        fprintf(stderr, "Send 1 message(s) : Receved %d message(s) : Exiting \n",i_numberReceived);
        close(sockfd);
        exit(0);    
        }
        }
    }
  
    //msg[n] = '\0';  /* end of string for printf() */
     

    close(sockfd);
    exit(0);
    return (EXIT_SUCCESS);
}

